package com.ruoyi.web.controller.kaoyan;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.kaoyan.domain.EducationCategory;
import com.ruoyi.kaoyan.domain.EducationForum;
import com.ruoyi.kaoyan.service.EducationCategoryService;
import com.ruoyi.kaoyan.service.EducationForumService;
import com.ruoyi.system.domain.SysFileInfo;
import com.ruoyi.system.service.ISysFileInfoService;
import com.ruoyi.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 前台考题问答、考研感想、每日心情管理类
 * @author carpediem
 */
@Api(tags = "前台考题问答、考研感想、每日心情", value = "前台考题问答、考研感想、每日心情")
@RequestMapping("/v2/forum")
@RestController
public class ForumApi extends BaseController {

    @Autowired
    private EducationForumService forumService;
    @Autowired
    private ISysUserService userService;
    @Autowired
    private ISysFileInfoService fileInfoService;
    @Autowired
    private EducationCategoryService categoryService;

    @ApiOperation("广场帖子列表")
    @PostMapping("/forumList")
    public AjaxResult forumList(Long categoryId, String title, Long userId){
        LambdaQueryWrapper<EducationForum> wrapper = new LambdaQueryWrapper<EducationForum>()
                .like(StringUtils.isNotBlank(title), EducationForum::getTitle, title)
                .eq(StringUtils.isNotNull(categoryId), EducationForum::getCategoryId, categoryId)
                .eq(EducationForum::getVerFlag, 1);
        if(StringUtils.isNotNull(userId)){
            wrapper.eq(EducationForum::getUserId, userId);
        }
        Page<EducationForum> forumPage = forumService.page(startPagePlus(), wrapper);
        List<EducationForum> forumList = forumPage.getRecords();
        Set<Long> collect = forumList.stream().map(EducationForum::getCategoryId).collect(Collectors.toSet());
        Set<Long> collect1 = forumList.stream().map(EducationForum::getUserId).collect(Collectors.toSet());
        Set<String> collect2 = forumList.stream().map(EducationForum::getImageGroupId).collect(Collectors.toSet());
        //用户名
        List<SysUser> userList = userService.list(new LambdaQueryWrapper<SysUser>()
                .in(StringUtils.isNotEmpty(collect1), SysUser::getUserId, collect1));
        Map<Long, String> userNameMap = userList.stream().collect(Collectors.toMap(SysUser::getUserId, SysUser::getUserName));
        //类目名
        List<EducationCategory> categories = categoryService.list(new LambdaQueryWrapper<EducationCategory>()
                .in(StringUtils.isNotEmpty(collect), EducationCategory::getId, collect));
        Map<Long, String> categoryMap = categories.stream().collect(Collectors.toMap(EducationCategory::getId, EducationCategory::getName));
        //文件地址
        Map<String, List<SysFileInfo>> filePathMapList = fileInfoService.getFilePathMapList(collect2);
        forumList.forEach(e -> {
            e.setUserName(userNameMap.get(e.getUserId()));
            e.setCategoryName(categoryMap.get(e.getCategoryId()));
            e.setImgPathList(filePathMapList.get(e.getImageGroupId()));
        });
        return AjaxResult.ok().page(forumPage, forumList).build();
    }


    @ApiOperation("发布帖子")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody EducationForum forum){
        if (StringUtils.checkObjAllFieldsIsNull(forum)){
            return AjaxResult.warn("内容为空");
        }
        boolean save = forumService.saveOrUpdate(forum);
        return toAjax(save);
    }

    @ApiOperation("删除帖子")
    @PostMapping("/remove")
    public AjaxResult removeForum(Long id){
        boolean remove = forumService.removeById(id);
        return toAjax(remove);
    }
}
